package cn.qiao.demo.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 自定义环境处理，在运行SpringApplication之前加载任意配置文件到Environment环境中
 */
@Component
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

    //Properties对象
    private final Properties properties = new Properties();

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        //自定义配置文件
        String[] profiles = {
                "spring.factories",
        };

        //循环添加

        HashMap<String, Object> map = new HashMap<>();
        for (PropertySource<?> ps : environment.getPropertySources()) {
            if (ps.getName().equals("configurationProperties")) {
                Object source = ps.getSource();

            }
            if (ps instanceof OriginTrackedMapPropertySource) {
                OriginTrackedMapPropertySource source = (OriginTrackedMapPropertySource) ps;
                for (String name : source.getPropertyNames()) {
                    Object value = source.getProperty(name);
                    System.out.println("value ======= " + value);
                    if (value instanceof String) {
                        String str = (String) value;
                        if (str.startsWith("mpen:")) { //mpen:  需要处理的value的前缀
                            map.put(name,getValue(str));     //此处的12345可自定义为对value的处理
                        }
                    }
                }
            }
        }

        Map<String, Object> systemEnvironment = environment.getSystemEnvironment();
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Object> next = iterator.next();
            System.out.println(next.getKey());
            System.out.println(next.getValue());
        }

        Map<String, Object> systemProperties = environment.getSystemProperties();
        Iterator<Map.Entry<String, Object>> iterator1 = systemEnvironment.entrySet().iterator();
        while (iterator1.hasNext()){
            Map.Entry<String, Object> next = iterator1.next();
            System.out.println("---------------------------------------------");
            System.out.println(next.getKey());
            System.out.println(next.getValue());
        }


        // 将处理的数据放入环境变量，并处于第一优先级上 (这里一定要注意，覆盖其他配置)
        if (map != null) {
            environment.getPropertySources().addFirst(new MapPropertySource("prefixer", map));
        }
    }

    private String getValue(String value){
        String[] split = value.split("mpen:");
        String s = split[1];
        return "";


    }
}